<?php
	include_once('header.php');
	// start 删除评论
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$id_comment = $_POST['id_comment_hidden'];
		$query_del_comment = "delete from `comment` where `id`=".$id_comment;
		$result_del_comment = $mysql_connection->query($query_del_comment);
	}
	// end 删除评论
	// start 取得当前页数
	if (isset($_GET['page'])) {
		$num_page_comment = floor($_GET['page']);
	} else {
		$num_page_comment = 1;
	}
	// end 取得当前页数

	// start 取得当前评论数量 和 页面数量
	if (isset($_GET['per_count'])) {
		$count_per_page = $_GET['per_count'];
	} else {
		$count_per_page = 5;
	}
	$count_comment = count_table($mysql_connection, 'comment');
	$count_page_comment = ceil($count_comment/$count_per_page);
	// end 取得当前评论数量 和 页面数量

	// start 根据当前页面取得 comment 表内容
	$offset_comment = ($num_page_comment-1)*$count_per_page;
	$query_comment = "select * from `comment` order by `post_time` desc limit ".$offset_comment.",".$count_per_page;
	$result_comment = $mysql_connection->query($query_comment);
	$count_result_comment = $result_comment->num_rows;
	// end 根据当前页面取得 comment 表内容
?>
<div id="content_b">
	<!-- start 上面关于显示页面页数的信息 -->
	<div id="header_edit_comment">
		<?php
			// start 显示当前显示的评论范围和总评论数量
			if (($num_page_comment)*$count_per_page<=$count_comment) {
				echo '第 '.(($num_page_comment-1)*$count_per_page).'- '.($num_page_comment*$count_per_page).' 项, 共 '.$count_comment.' 项';
			} else {
				echo '第 '.(($num_page_comment-1)*$count_per_page).'- '.($count_comment).' 项, 共 '.$count_comment.' 项';
			}
			// end 显示当前显示的评论范围和总评论数量
			// start 显示页面导航
		?>
		<span class="space2"></span>
		<!-- start 显示上一页 -->
		<?php
			if ($num_page_comment<=1) {
				echo '上一页';
			} else {
			?>
			<a href="edit_comment.php?page=<?php echo $num_page_comment-1;?>&per_count=<?php echo $count_per_page;?>">上一页</a>
			<?php
			}
		?>
		<!-- end 显示上一页 -->
		<span class="space2"></span>第<?php
			if ($num_page_comment!=1) {
			?>
			<span class="num_page_edit_comment"><a href="edit_comment.php?page=1&per_count=<?php echo $count_per_page;?>">1</a></span>
			<?php
			} else {
				echo '<span class="num_page_edit_comment">1</span>';
			}

			if (($count_page_comment-2)>3) {
				if ($num_page_comment<=4 or $num_page_comment>=($count_page_comment-3)) {
					if ($num_page_comment<=($count_page_comment/2)) {
						for ($n=2; $n<=4; $n++) {
							if ($num_page_comment!=$n) {
								echo '<span class="num_page_edit_comment"><a href="edit_comment.php?page='.$n.'&per_count='.$count_per_page.'">'.($n).'</a></span>';
							} else {
								echo '<span class="num_page_edit_comment">'.$n.'</span>';
							}
						}
					?>
					<span class="num_page_edit_comment">...</span>
					<?php
					} else {
					?>
					<span class="num_page_edit_comment">...</span>
					<?php
						for ($p=$count_page_comment-3; $p<=$count_page_comment-1; $p++) {
							if ($num_page_comment!=$p) {
								echo '<span class="num_page_edit_comment"><a href="edit_comment.php?page='.$p.'&per_count='.$count_per_page.'">'.$p.'</a></span>';
							} else {
								echo '<span class="num_page_edit_comment">'.$p.'</span>';
							}
						}
					}
				} else {
				?>
				<span class="num_page_edit_comment">...</span>
				<?php
					for ($r=($num_page_comment-1); $r<=($num_page_comment+1); $r++) {
						if($num_page_comment==$r) {
							echo '<span class="num_page_edit_comment">'.$r.'</span>';
						} else {
						?>
						<span class="num_page_edit_comment"><a href="edit_comment.php?page=<?php echo $r;?>&per_count=<?php echo $count_per_page;?>"><?php echo $r;?></a></span>
						<?php
						}
					}
				?>
				<span class="num_page_edit_comment">...</span>
				<?php
				}
			} else if (0<($count_page_comment-2) and ($count_page_comment-2)<=3) {
				for ($m=1; $m<=($count_page_comment-2); $m++) {
					if ($num_page_comment==($m+1)) {
						echo '<span class="num_page_edit_comment">'.($m+1).'</span>';
					} else {
						echo '<span class="num_page_edit_comment"><a href="edit_comment.php?page='.($m+1).'&per_count='.$count_per_page.'">'.($m+1).'</a></span>';
					}
				}
			}
			
			if ($count_page_comment!=1) {
				if ($num_page_comment!=$count_page_comment) {
				?>
				<span class="num_page_edit_comment"><a href="edit_comment.php?page=<?php echo $count_page_comment;?>&per_count=<?php echo $count_per_page;?>"><?php echo $count_page_comment;?></a></span>
				<?php
				} else {
					echo '<span class="num_page_edit_comment">'.$count_page_comment.'</span>';
				}
			}
			// end 显示页面导航
		?>页<span class="space2"></span>
		<!-- start 显示下一页 -->
		<?php
			if ($num_page_comment>=$count_page_comment) {
				echo '下一页';
			} else {
			?>
			<a href="edit_comment.php?page=<?php echo $num_page_comment+1;?>&per_count=<?php echo $count_per_page;?>">下一页</a>
			<?php
			}
		?>
		<!-- end 显示下一页 -->

		<!-- start 设定每页显示的评论数量 -->
		<span class="space2"></span>每页显示评论数：
		<select onchange='document.location.href=this.options[this.selectedIndex].value;'>
			<?php
				for ($u=1; $u<=625; $u=$u*5) {
					if ($count_per_page==$u) {
					?>
					<option selected="selected" value="edit_comment.php?page=1&per_count=<?php echo $u;?>"><?php echo $u;?></option>
					<?php
					} else {
					?>
					<option value="edit_comment.php?page=1&per_count=<?php echo $u;?>"><?php echo $u;?></option>
					<?php
					}
				}
			?>
		</select>
		<!-- end 设定每页显示的评论数量 -->
	</div>
	<!-- end 上面关于显示页面页数的信息 -->
	<div id="content_edit_comment">
		<table border="1" width="100%">
			<tr><td>昵称</td><td>评论</td><td>所在日志</td><td align="right">删除</td></tr>
			<?php
				for ($i=1; $i<=$count_result_comment; $i++) {
					$each_result_comment = $result_comment->fetch_assoc();
				?>
				<form action="<?php echo $_SERVER['PHP_SELF'];?>?message_box=此评论已经删除" method="post" onsubmit="return confirm('确定要删除吗?');">
				<tr><td><?php echo $each_result_comment['nickname'];?></td>
				<td><?php echo $each_result_comment['content'];?></td>
				<td><?php
					$article_id = $each_result_comment['articles_id'];
					$query_title_article = "select `title` from `articles` where `id`=".$article_id;
					$result_title_article = $mysql_connection->query($query_title_article);
					$result_title_article = $result_title_article->fetch_assoc();
					$title_article = $result_title_article['title'];
					echo $title_article;
				?></td>
				<td align="right"><input type="hidden" name="id_comment_hidden" value="<?php echo $each_result_comment['id'];?>" /><input type="submit" value="删除" /></td>
				</tr></form>
				<?php
				}
			?>
		</table>
	</div>
</div>
<?php include_once('footer.php');?>
